$(function () {
  App.loginOut();
  // var size = 2; //一页显示的菜品数量
  var pageAllNum = 1; //控制全部商品的页码
  var pageAllTotal = ""; //控制全部商品的总页数
  var pageCategoryNum = 1; //控制分类商品的页码
  var pageCategoryTotal = ""; // 控制分类商品的总页数
  var flag_scroll = true; //控制滚动
  var sear_btnmiss = true; //搜索页面点击取消时
  var curTableInit = JSON.parse(localStorage.getItem("curTable") || "{}");
  var addOrder = App.GetQueryValue("addOrder");
  if (addOrder) {
    $(".payBtns").addClass("hidden");
  }

  $(".backtotable").off("click").on("click", function () {
    App.ajax("ss_wx/unlock_table", "post", {
      "table_id": curTableInit.table_id,
    }, function (res) {
      if (res.flag == true) {
        location.href = "../pages/Table.html";
      } else {
        App.msg('未获取到桌台信息')
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true);
  });

  if (JSON.stringify(curTableInit) == "{}") {
    App.confirm("未选取桌台，前往桌台", function () {
      location.href = "../pages/Table.html";
    }, "no", 1)
  } else {
    App.ajax("ss_wx/lock_table", "post", {
      "table_id": curTableInit.table_id,
    }, function (res) {
      if (res.flag == true) {} else {
        App.msg("桌台锁失败");
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true);
    // if (curTableInit.state == 4) {
    //   App.confirm("该桌台已结账，请先清台", function () {
    //     location.href = "../pages/Table.html"
    //   }, 1)
    // }
    $(".order .table_name").html("当前桌台：" + curTableInit.name);
  }
  // App.loadTop();
  var userInfo = JSON.parse(localStorage.getItem("logInfo"));
  $(".top .userInfo").html(userInfo.login);

  //控制搜索页面和点菜页面的切换
  var page = "order";
  //加载左侧菜品类别
  $(".order>.main .main-left").on("click", "li", function () {
    $(this).addClass("active").siblings().removeClass("active");
  });
  App.ajax("ss_wx/get_pos_category", "get", {}, function (res) {
    if (res.flag == true) {
      var html = '<li data-categoryid="all" class="active ">全部商品</li>';
      $.each(res.category, function (i, v) {
        html += '<li data-categoryid="' + v.id + '">' + v.name + '<span class="typeCount hidden">0</span></li>'
      });
      $(".order .main .main-left").html(html);
      getCart();
    } else {
      App.msg(res.msg)
    }
    if (!res.TokenTrue) {
      App.msg("Token失效，即将跳转到登录页面", function () {
        setTimeout(function () {
          location.href = "../login.html";
        }, 1000)
      })
    }
  }, "", true);
  // var category_leixing="";
  //初始获取所有菜品
  getAll("Dish", "", "all", true);

  function getAll(content, searKey, id, loading) {
    App.ajax("ss_wx/get_pos_all_product", "get", {
      // size:size,
      page: pageAllNum
    }, function (res) {
      if (res.flag == true) {
        pageAllTotal = res.totalPages;
        // category_leixing=res.category_leixing;
        if (content == "Dish") {
          fillDish(res.products, "", id);
        }
        if (content == "search") {
          fillSearList(res.products, "", searKey);
        }
      } else {
        App.msg(res.msg)
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true)
  }

  //点击加载类别对应菜品并缓存
  $(".order .main .main-left").on("click", "li", function () {
    $(".order .mainBox .main-content")[0].scrollTop = 0;
    var id = $(this).data("categoryid");
    // var category_product = JSON.parse(localStorage.getItem("category_product") || "[]");
    // var category_product_id = [];
    // if (category_product.length > 0) {
    //   category_product_id = category_product.filter(function (v, i) {
    //     return v.c_id == id;
    //   });
    // }
    // $(".order .mainBox .main-content").off("scroll");
    // $(".order .mainBox .main-content").trigger("scroll");
    flag_scroll = false;
    if (page == "order") {
      if (id == "all") {
        pageAllNum = 1;
        getAll("Dish", "", id);
      } else {
        // if (category_product.length > 0 && category_product_id.length > 0) {
        //   fillDish(category_product_id);
        // } else {
        pageCategoryNum = 1;
        App.ajax("ss_wx/get_pos_category_product", "post", {
          "c_id": id,
          // size:size,
          page: pageCategoryNum
        }, function (res) {
          pageCategoryTotal = res.totalPages;
          if (res.flag == true) {
            //缓存代码
            // fillDish(res.category_product,category_product);
            fillDish(res.category_product, "", id);
            // localStorage.setItem("category_product", JSON.stringify(category_product));
          } else {
            App.msg(res.msg)
          }
          if (!res.TokenTrue) {
            App.msg("Token失效，即将跳转到登录页面", function () {
              setTimeout(function () {
                location.href = "../login.html";
              }, 1000)
            })
          }
        }, "", true)
        // }
      }
    } else {
      // if (id == "all") {
      //   getAll("search");
      // } else {
      //   // if (category_product.length > 0 && category_product_id.length > 0) {
      //   //   fillSearList(category_product_id);
      //   // } else {
      //   App.ajax("ss_wx/get_pos_category_product", "post", {
      //     "c_id": id,
      //   }, function (res) {
      //     if (res.flag == true) {
      //       fillSearList(res.category_product);
      //       // fillSearList(res.category_product,category_product);
      //       // localStorage.setItem("category_product", JSON.stringify(category_product));
      //     } else {
      //       App.msg(res.msg, function () {
      //         setTimeout(function () {
      //           location.href = "../login.html";
      //         }, 1000)
      //       })
      //     }
      //   })
      //   // }
      // }
    }
  });

  //菜品展示滚动加载
  var height = $(".order .mainBox .main-content").height();
  var padding = parseFloat($(".order .mainBox .main-content").css("padding-top"));
  $(".order .mainBox .main-content").on("scroll", function () {
    var id = $(".main-left li.active").data("categoryid");
    var scrollHeight = $(".order .mainBox .main-content")[0].scrollHeight;
    var scrollTop = $(".order .mainBox .main-content")[0].scrollTop;
    // console.log(scrollHeight);
    // console.log(scrollTop);
    // console.log(height);
    // console.log(padding);
    // console.log(scrollTop + height + 2 * padding);

    if (flag_scroll) {
      if (scrollTop + height + 2 * padding >= scrollHeight - 1) {
        // alert("滚动条到底部了");
        if (id == "all") {
          // alert(pageAllNum);
          // alert(pageAllTotal);
          if (pageAllNum <= pageAllTotal) {
            getAll("Dish", "", id, true);
          } else {
            App.msg("已加载全部菜品");
          }
        } else {
          if (pageCategoryNum <= pageCategoryTotal) {
            App.ajax("ss_wx/get_pos_category_product", "post", {
              "c_id": id,
              page: pageCategoryNum
            }, function (res) {
              if (res.flag == true) {
                //缓存代码
                // fillDish(res.category_product,category_product);
                fillDish(res.category_product, "", id);
                // localStorage.setItem("category_product", JSON.stringify(category_product));
              } else {
                App.msg(res.msg)
              }
              if (!res.TokenTrue) {
                App.msg("Token失效，即将跳转到登录页面", function () {
                  setTimeout(function () {
                    location.href = "../login.html";
                  }, 1000)
                })
              }
            }, "", true)
          } else {
            App.msg("已加载全部菜品");

          }
        }
      }
    } else {
      flag_scroll = true;
    }
  });


  //搜索菜品滚动加载

  //填充菜品
  function fillDish(res, storage, id) {
    var html = "";
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    if (cart.length > 0) {
      $.each(res, function (i, v) {
        var Num = 0;
        var Num0 = 0; //用来计算沽清
        $.each(cart, function (j, k) {
          if (v.p_id == k.p_id && v.default_code == k.default_code) {
            Num += k.num * 1;
            v.num = Num;
            if (k.rsta != 1) {
              Num0 += k.num * 1;
            }
            if (v.sale_count !== "") {
              if (v.sale_count - Num0 <= 0) {
                v.sale_count = 0;
              } else {
                v.sale_count = v.sale_count - Num0
              }
            }
          }
        });
      })
    }
    $.each(res, function (i, v) {
      html += "<li data-dishinfo='" + JSON.stringify(v) + "'><div class='dishName'>" + v.name + "</div>" +
          (v.start_sale_date ? "<div style='color:red;'>" + v.start_sale_date + "-" + v.end_sale_date + "</div>" : "") +
          "<div>" + (v.price ? "￥" + v.price + "/" + v.uom : "") + (v.sale_count === "" ? "" : "<span class='sale_count'>" +
              v.sale_count + "</span>") + "</div><span class='dishCount hidden'>" + (v.num || 0) + "</span>" +
          (v.sale_count === "" ? "" : "<div class='sold_out " + (v.sale_count <= 0 ? "" : "hidden") +
              "'><span class='out_text'>已售罄</span></div>") + "</li>";
      if (storage) {
        storage.push(v);
      }
    });
    // $(".order .mainBox .main-content").html(html);
    if (sear_btnmiss == false) {
      $(".order .mainBox .main-content").html(html);
      sear_btnmiss = true;
      pageAllNum = 2;
    } else {
      if (id == "all") {
        if (pageAllNum == 1) {
          $(".order .mainBox .main-content").html(html);
        } else {
          $(".order .mainBox .main-content").append(html);
        }
        pageAllNum += 1;
      }
      if (id != "all") {
        if (pageCategoryNum == 1) {
          $(".order .mainBox .main-content").html(html);
        } else {
          $(".order .mainBox .main-content").append(html);
        }
        pageCategoryNum += 1;
      }
    }

    $(".order .mainBox .main-content .dishCount").each(function (a, b) {
      var dishCount = $(this).html();
      if (dishCount > 0) {
        $(this).removeClass("hidden");
      } else {
        $(this).addClass("hidden")
      }
    })
  }
  //填充搜索列表
  function fillSearList(res, storage, searKey) {
    var html = "";
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    if (cart.length > 0) {
      $.each(res, function (i, v) {
        var Num = 0;
        var Num0 = 0; //用来计算沽清
        $.each(cart, function (j, k) {
          if (v.p_id == k.p_id && v.default_code == k.default_code) {
            Num += k.num * 1;
            v.num = Num;
            if (k.rsta != 1) {
              Num0 += k.num * 1;
            }
            if (v.sale_count !== "") {
              if (v.sale_count - Num0 <= 0) {
                v.sale_count = 0;
              } else {
                v.sale_count = v.sale_count - Num0
              }
            }
          }
        });
      })
    }
    // if (searKey) {
    //   searKey = searKey.toLowerCase();
    //   res = res.filter(function (a, b) {
    //     return (a.name.indexOf(searKey) != -1 || a.pinyin_str.indexOf(searKey) != -1)
    //   });
    //   if (res.length == 0) {
    //     html += "<div class='text-center'>未搜索到相关菜品。</div>"
    //   }
    // }
    $.each(res, function (i, v) {
      html += "<li data-dishinfo='" + JSON.stringify(v) + "'><div class='dishName'>" + v.name + "</div><div>" + (v.price ? "￥" + v.price + "/" + v.uom : "") + "</div>" + (v.sale_count === "" ? "" : "<div>剩<span class='sale_count'>" + v.sale_count + "</span>" + v.uom + "</div><div class='sold_out" + (v.sale_count <= 0 ? "" : " hidden") + "'><span class='out_text'>已售罄</span></div>") + "</li>";
      if (storage) {
        storage.push(v);
      }
    });
    $(".order .searCon .searList").html(html);
  }

  //返回点菜页面加载已点菜品
  getCart();

  function getCart() {
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    var totalInfo = JSON.parse(localStorage.getItem("curTable") || "{}").totalInfo || {};
    if (cart.length > 0) {
      var html = "";
      $.each(cart, function (i, v) {
        var pprice = parseFloat(v.price).toFixed(2);
        var zfprice = parseFloat(v.ZFprice).toFixed(2);
        var totalTCPrice = parseFloat(v.totalTCPrice).toFixed(2);
        html += "<li data-dishes=\'" + JSON.stringify(v) + "\' data-price='" + (v.rsta == 1 ?
            v.price : (v.combo_details && v.combo_details.length > 0 ? totalTCPrice : v.cuisine && v.cuisine.length > 0 ?
                (pprice + (zfprice ? zfprice * 1 : 0)) : pprice)) + "' data-defaultcode='" +
            v.default_code + "' data-pid='" + v.p_id + "' data-cid='" + v.c_id + "'>" + (v.temporary_flag ?
                '<div class="ls_dish">临时</div>' : '') + "<div class='pack hidden'>打包</div>" +
            "<div class='waitCall hidden'>等叫</div><div class='dishName'>" + v.name +
            (v.ZFname ? "<span style='color:#f40'> | " + v.ZFname+"</span>" : (v.combo_details && v.combo_details.length > 0 ?
                "<a style='margin-left:8px;' class='btn btn-sm btn-warning btn-details' data-toggle='popover' >套餐详情</a>" : ""))
            + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle " +
            (v.rsta == 1 ? 'hidden' : '') + "'></i><span class='dishNum'>" + v.num + "</span> " +
            "<i data-control='plus' class='iconfont icon-plus-circle" + (v.rsta == 1 ? 'hidden' : '') + "'></i> " +
            "</div><div class='dishPrice'>" + ((v.rsta == 1 ? pprice : (v.combo_details && v.combo_details.length > 0 ?
                totalTCPrice * 1 : v.cuisine && v.cuisine.length > 0 ?
                    (pprice + (zfprice ? zfprice : 0)) : pprice)) * v.num).toFixed(2) + "" +
            "</div></li>"
      });
      $(".order .main-right .dishList").html(html);
      $(".order .main-right .beizhu .totalCount").html((totalInfo.totalCount * 1));
      $(".order .main-right .beizhu .totalMon").html((totalInfo.totalMon * 1).toFixed(2));
      // javascript:showprice.ShowPrice((totalInfo.totalMon * 1).toFixed(2).toString());
      $(".order .main .main-left li").each(function (j, k) {
        var typeCount = 0;
        var that = this;
        $.each(cart, function (i, v) {
          if ($(that).data("categoryid") == v.c_id) {
            typeCount += v.num * 1;
          }
        })
        if (typeCount > 0) {
          $(that).find(".typeCount").html(typeCount).removeClass("hidden")
        }
      });
    }
  }

  // 下单页面搜索框
  $("#isearKey").on("keyup", function (e) {
    // if (e.keyCode == "13") {
      let id = $(".main-left li.active").data("categoryid");
      let searKey = $(this).val();
      App.ajax("ss_wx/search_product", "post", {
        "search_str": searKey,
      }, function (res) {
        if (res.flag == true) {
          if (res.category_product.length > 0) {
            $(".order .mainBox .main-content").empty();
            fillDish(res.category_product, "", id);
          } else {
            $(".order .mainBox .main-content").empty();
            App.msg("未搜索到相关产品");
          }
        } else {
          App.msg(res.msg);
        }
        if (!res.TokenTrue) {
          App.msg("Token失效，即将跳转到登录页面", function () {
            setTimeout(function () {
              location.href = "../login.html";
            }, 1000)
          })
        }
      }, "", true);
      // $("#isearKey").val("");
    // }
  });

  $("#searKey").on("keyup", function (e) {
    if (e.keyCode == "13") {
      $("#searBtn").trigger("click");
    }
  })
  //点击搜索页面输入框
  $("#searBtn").off("click").on("click", function () {
    var id = $(".order .main .main-left li.active").data("categoryid");
    var searKey = $("#searKey").val();
    //后端进行搜索
    App.ajax("ss_wx/search_product", "post", {
      "search_str": searKey,
    }, function (res) {
      if (res.flag == true) {
        if (res.category_product.length > 0) {
          fillSearList(res.category_product, "", "");
        } else {
          App.msg("未搜索到相关产品");
        }
      } else {
        App.msg(res.msg);
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true)
    $("#searKey").val("");
  })

  //点击搜索页菜品加入购物车，并缓存
  $(".mainBox .main-search .searList").off("click").on("click", "li", function () {
    var that = this;
    var dishinfo = $(this).data("dishinfo");
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    var sale_count = $(this).find(".sale_count").html() * 1;
    if (sale_count > 0 || isNaN(sale_count)) {
      if (dishinfo.cuisine && dishinfo.cuisine.length > 0) {
        //显示做法弹框
        $(".ZFModal .title .dishname").html(dishinfo.name);
        $(".ZFModal").removeClass("hidden");
        //显示做法
        var ZFHtml = "";
        $.each(dishinfo.cuisine, function (x, y) {
          ZFHtml += '<button class="btn1" data-zfinfo=\'' + JSON.stringify(y) + '\'>' + y.name + '</button>'
        })
        $(".ZFModal .Body").html(ZFHtml);
        $(".ZFModal .footer .price").html(dishinfo.price);

        //选择做法
        $(".ZFModal .Body").off("click").on("click", ".btn1", function () {
          var zfInfo = $(this).data("zfinfo");
          var zfmodal_price = $(".ZFModal .footer .price").html() * 1;
          if (!$(this).hasClass('active')) {
            $(".ZFModal .footer .price").html(parseFloat(zfmodal_price + zfInfo.price * 1).toFixed(2));
            $(this).addClass('active');
          } else {
            $(".ZFModal .footer .price").html(parseFloat(zfmodal_price - zfInfo.price).toFixed(2));
            $(this).removeClass('active');
          }
        })
        //点击做法确定按钮
        $(".ZFModal .footer .btn-sure").off("click").on("click", function () {
          $(".ZFModal").addClass("hidden");
          // if ($(".ZFModal .Body .active").length == 0) {
          //   delete dishinfo.ZFprice;
          //   delete dishinfo.ZFname
          // } else {
          //   dishinfo = dishinfo;
          // }
          var tastes = [];
          $(".ZFModal .Body .active").each(function (i, v) {
            tastes.push($(this).data("zfinfo"));
          })
          dishinfo.ZFprice = 0;
          dishinfo.ZFname = "";
          dishinfo.tastes = tastes;
          if (tastes.length == 0) {
            dishinfo.ZFprice = 0;
            dishinfo.ZFname = "";
          } else {
            tastes.forEach(function (v, i) {
              dishinfo.ZFprice += v.price * 1;
              dishinfo.ZFname += v.name + " ";
            })
          }
          var html = "<li data-price='" + (dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2) + "' data-pid='" + dishinfo.p_id + "' data-defaultcode='" + dishinfo.default_code + "' data-cid='" + dishinfo.c_id + "'><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div><div class='dishName'>" + dishinfo.name +"<span style='color:#f40'>"+(dishinfo.ZFname ? " | " + dishinfo.ZFname : "") + "</span></div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle'></i><span class='dishNum'>1</span> <i data-control='plus' class='iconfont icon-plus-circle'></i> </div><div class='dishPrice'>" + (dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2) + "</div></li>"
          $(".order .main-right .dishList").prepend(html);
          var totalCount = +$(".order .main-right .beizhu .totalCount").html();
          var totalMon = +$(".order .main-right .beizhu .totalMon").html();
          var dishCount = +$(that).find(".dishCount").html();
          var c_id = dishinfo.c_id;
          $(that).find(".dishCount").html(dishCount + 1);
          var dishName = dishinfo.name;
          var p_id = dishinfo.p_id;
          $(".order .mainBox .main-content li").each(function (a, b) {
            var dishInfo = $(this).data("dishinfo");
            var name = $(this).find(".dishName").html();
            var dishid = dishInfo.p_id;
            var dishCount = +$(this).find(".dishCount").html();
            if (dishid == p_id) {
              $(this).find(".dishCount").html(dishCount + 1);
              $(this).find(".dishCount").removeClass("hidden");
            }
          })
          $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
          $(".order .main-right .beizhu .totalMon").html((totalMon + dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2));
          // javascript:showprice.ShowPrice((totalMon + dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2).toString());
          $(".order .main .main-left li").each(function (j, k) {
            if ($(this).data("categoryid") == c_id) {
              var typeCount = +$(this).find(".typeCount").html();
              $(this).find(".typeCount").html(typeCount + 1);
              $(this).find(".typeCount").removeClass("hidden");
            }
          });
          dishinfo.num = 1;
          cart.unshift(dishinfo);
          var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
          curTable.cart = cart;
          localStorage.setItem("curTable", JSON.stringify(curTable));
          var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          if (tableInfos && tableInfos.length > 0) {
            $.each(tableInfos, function (j, k) {
              if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                // tableInfos[j] = curTable;
                tableInfos.splice(j, 1, curTable);
              }
            })
            localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
          }
          if (!isNaN(sale_count)) {
            sale_count -= 1;
            if (sale_count <= 0) {
              sale_count = 0;
              $(that).find(".sold_out").removeClass("hidden");
            }
            $(that).find(".sale_count").html(sale_count);
          }

        })
      } else if (dishinfo.combo_details && dishinfo.combo_details.length > 0) {
        // if(App.timeBetween(dishinfo.start_sale_date,dishinfo.end_sale_date)){
        //显示套餐弹框
        $(".TCModal .title .dishname").html(dishinfo.name);
        $(".TCModal").removeClass("hidden");
        //显示做法
        var TCHtml = "";
        var MRprice = 0;
        $.each(dishinfo.combo_details, function (x, y) {
          // TCHtml += '<button class="btn1 ' + (y.category_leixing == "默认全选" ? "active" : "") + '" data-tcinfo=\'' + JSON.stringify(y) + '\'>' + y.name + (y.category_leixing == "默认全选" ? "<i class='iconfont icon-bixuan'></i>" : " ") + '</button>'
          TCHtml += '<div data-tcinfo=\'' + JSON.stringify(y) + '\' class="tcItem"><div class="category_name">' + y.category_name + (y.category_leixing == "默认全选" ? '(必选)' : '(任选' + y.select_num + '份)') + '</div><div class="tcDisheList">';
          $.each(y.menu_list, function (a, b) {
            if (y.category_leixing == "默认全选") {
              MRprice += b.price;
            }
            TCHtml += '<div class="tcDishe"><span class="dishname">' + b.name + '</span><span class="price">￥'
                + b.price + '</span><div class="control" data-dishinfo1=\'' + JSON.stringify(b) + '\'> ' +
                '<span class="iconfont icon-minus-circle minus ' + (y.category_leixing == "默认全选" ? "hidden" : "") +
                '"></span> <span class="num">' + ((y.category_leixing == "默认全选" ? "1" : "0")) + '' +
                '</span><span class="iconfont icon-plus-circle ' + (y.category_leixing == "默认全选" ? "hidden" : "") +
                ' add"></span></div></div>'
          })
          TCHtml += '</div></div>';

        });

        $(".TCModal .Body").html(TCHtml);
        $(".TCModal .footer .price").html(dishinfo.extend_combo_flag ? dishinfo.price.toFixed(2) : MRprice.toFixed(2));
        dishinfo.totalTCPrice = (dishinfo.extend_combo_flag ? dishinfo.price.toFixed(2) : MRprice.toFixed(2));
        //选择套餐(套餐的另一种写法)
        // $(".TCModal .Body").off("click").on("click", ".btn1", function () {
        //   var totalTCPrice = $(".TCModal .footer .price").html();
        //   var tcInfo = $(this).data("tcinfo");
        //   if (tcInfo.category_leixing == "默认全选") {
        //     App.msg("所选菜品为该套餐必选菜品");
        //   } else {
        //     // $(this).addClass("active").siblings().removeClass("active");
        //     $(this).toggleClass("active");
        //     if ($(this).hasClass("active")) {
        //       $(".TCModal .footer .price").html(totalTCPrice * 1 + tcInfo.price * 1);
        //       totalTCPrice = totalTCPrice * 1 + tcInfo.price * 1;
        //     } else {
        //       $(".TCModal .footer .price").html(totalTCPrice * 1 - tcInfo.price * 1);
        //       totalTCPrice = totalTCPrice * 1 - tcInfo.price * 1;
        //     }
        //   }
        //   dishinfo.totalTCPrice = totalTCPrice;
        // })
        //选择套餐（套餐的另一种写法）//点击加号
        $(".TCModal .Body").off("click").on("click", ".add", function () {
          var dishinfo1 = $(this).parents(".control").data("dishinfo1");
          var tcprice = $(".TCModal .footer .price").html();
          //本菜品可选的套餐的最大数量
          var dish_select_num = dishinfo1.select_num;
          var tcinfo = $(this).parents(".tcItem").data("tcinfo");
          //本套餐的这个类别最多可选的数量
          var choosedNum = 0;
          $(this).parents(".tcItem").find(".num").each(function (x, y) {
            if ($(this).html() > 0) {
              choosedNum += 1;
            }
          })
          var tc_select_num = tcinfo.select_num;
          var num = $(this).prev().html() * 1;
          if (num <= 0) {
            if (choosedNum < tc_select_num) {
              $(this).prev().html(num + 1);
              $(".TCModal .footer .price").html(parseFloat(tcprice * 1 + dishinfo1.price).toFixed(2));
              dishinfo.totalTCPrice = parseFloat(tcprice * 1 + dishinfo1.price).toFixed(2);
              dishinfo1.categoryNum = 0;
              dishinfo1.categoryNum += 1;
              dishinfo1.choosedNum = num + 1;
              $(this).parents(".control").attr("data-dishinfo1", JSON.stringify(dishinfo1));
            } else {
              App.msg(tcinfo.category_name + "只能任选" + tcinfo.select_num + "份");
            }
          } else {
            if (num < dish_select_num) {
              $(this).prev().html(num + 1);
              $(".TCModal .footer .price").html(parseFloat(tcprice * 1 + dishinfo1.price).toFixed(2));
              dishinfo.totalTCPrice = parseFloat(tcprice * 1 + dishinfo1.price).toFixed(2);
              dishinfo1.choosedNum = num + 1;
              $(this).parents(".control").attr("data-dishinfo1", JSON.stringify(dishinfo1));
            } else {
              App.msg("本菜品只可选择" + dish_select_num + "份")
            }

          }
        })
        // //点击减号
        $(".TCModal .Body").on("click", ".minus", function () {
          var dishinfo1 = $(this).parents(".control").data("dishinfo1");
          var tcprice = $(".TCModal .footer .price").html();
          var num = $(this).next().html() * 1;
          if (num <= 0) {
            return;
          } else {
            $(this).next().html(num - 1);
            $(".TCModal .footer .price").html(parseFloat(tcprice * 1 - dishinfo1.price).toFixed(2));
            dishinfo.totalTCPrice = parseFloat(tcprice * 1 - dishinfo1.price).toFixed(2);
            if (num - 1 <= 0) {
              dishinfo1.categoryNum = 0;
            }
            dishinfo1.choosedNum = num - 1;
            $(this).parents(".control").attr("data-dishinfo1", JSON.stringify(dishinfo1));
          }
        })
        //点击做法确定按钮
        $(".TCModal .footer .btn-sure").off("click").on("click", function () {
          if (dishinfo.extend_combo_flag) {
            dishinfo.totalTCPrice = dishinfo.price;
          }
          dishinfo.combo_details1 = [];
          var flag1 = "";
          $(".TCModal .Body .tcItem").each(function (x, y) {
            var tcinfo = $(this).data("tcinfo");
            if (tcinfo.category_leixing == "默认全选") {
              flag1 += "true";
            }
            var tcChoosedNum = 0;
            if (tcinfo.category_leixing == "客人自选") {
              tcinfo.menu_list = [];
              $(this).find('.control').each(function (a, b) {
                var dishinfo1 = $(this).data("dishinfo1");
                if (dishinfo1.categoryNum >= 1) {
                  tcinfo.menu_list.push(dishinfo1);
                }
                tcChoosedNum += (dishinfo1.categoryNum ? dishinfo1.categoryNum * 1 : 0);

              })
              if (tcChoosedNum == tcinfo.select_num) {
                flag1 += "true";
              } else {
                flag1 += "false"
                App.msg(tcinfo.category_name + "需任选" + tcinfo.select_num + "份");
                return false;
              }
            }
            dishinfo.combo_details1.push(tcinfo);
          })
          if (flag1.indexOf("false") == -1) {
            $(".TCModal").addClass("hidden");
            var html = "<li data-dishes=\'" + JSON.stringify(dishinfo) + "\' data-price='" + dishinfo.totalTCPrice + "' data-pid='" + dishinfo.p_id + "' data-cid='" + dishinfo.c_id + "'><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div><div class='dishName'>" + dishinfo.name + (dishinfo.combo_details && dishinfo.combo_details.length > 0 ? "<a style='margin-left:8px;' class='btn btn-details btn-warning btn-sm' data-toggle='popover'>套餐详情</a>" : "") + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle'></i><span class='dishNum'>1</span> <i data-control='plus' class='iconfont icon-plus-circle'></i> </div><div class='dishPrice'>" + (dishinfo.totalTCPrice * 1).toFixed(2) + "</div></li>"
            $(".order .main-right .dishList").prepend(html);
            var totalCount = +$(".order .main-right .beizhu .totalCount").html();
            var totalMon = +$(".order .main-right .beizhu .totalMon").html();
            var dishCount = +$(that).find(".dishCount").html();
            var c_id = dishinfo.c_id;
            $(that).find(".dishCount").html(dishCount + 1);
            var dishName = dishinfo.name;
            var p_id = dishinfo.p_id;
            $(".order .mainBox .main-content li").each(function (a, b) {
              var dishInfo = $(this).data("dishinfo");
              var name = $(this).find(".dishName").html();
              var dishid = dishInfo.p_id;
              var dishCount = +$(this).find(".dishCount").html();
              if (dishid == p_id) {
                $(this).find(".dishCount").html(dishCount + 1);
                $(this).find(".dishCount").removeClass("hidden");
              }
            })
            $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
            $(".order .main-right .beizhu .totalMon").html((totalMon + dishinfo.totalTCPrice * 1).toFixed(2));
            // javascript:showprice.ShowPrice((totalMon + dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2).toString());
            $(".order .main .main-left li").each(function (j, k) {
              if ($(this).data("categoryid") == c_id) {
                var typeCount = +$(this).find(".typeCount").html();
                $(this).find(".typeCount").html(typeCount + 1);
                $(this).find(".typeCount").removeClass("hidden");
              }
            });
            dishinfo.num = 1;
            cart.unshift(dishinfo);
            var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
            curTable.cart = cart;
            localStorage.setItem("curTable", JSON.stringify(curTable));
            var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
            if (tableInfos && tableInfos.length > 0) {
              $.each(tableInfos, function (j, k) {
                if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                  // tableInfos[j] = curTable;
                  tableInfos.splice(j, 1, curTable);
                }
              })
              localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
            }
          }
          if (!isNaN(sale_count)) {
            sale_count -= 1;
            if (sale_count <= 0) {
              sale_count = 0;
              $(that).find(".sold_out").removeClass("hidden");
            }
            $(that).find(".sale_count").html(sale_count);
          }
        })
        // }else{
        //   App.msg("还没到优惠时间哦。");
        // }
      } else {
        var html = "<li data-price='" + dishinfo.price + "' data-pid='" + dishinfo.p_id + "' data-cid='" + dishinfo.c_id + "'><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div><div class='dishName'>" + dishinfo.name + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle'></i><span class='dishNum'>1</span> <i data-control='plus' class='iconfont icon-plus-circle'></i> </div><div class='dishPrice'>" + (dishinfo.price * 1).toFixed(2) + "</div></li>"
        $(".order .main-right .dishList").prepend(html);
        var totalCount = +$(".order .main-right .beizhu .totalCount").html();
        var totalMon = +$(".order .main-right .beizhu .totalMon").html();
        var dishCount = +$(this).find(".dishCount").html();
        var c_id = dishinfo.c_id;
        $(this).find(".dishCount").html(dishCount + 1);
        var dishName = dishinfo.name;
        var p_id = dishinfo.p_id;
        $(".order .mainBox .main-content li").each(function (a, b) {
          var dishInfo = $(this).data("dishinfo");
          var name = $(this).find(".dishName").html();
          var dishid = dishInfo.p_id;
          var dishCount = +$(this).find(".dishCount").html();
          if (dishid == p_id) {
            $(this).find(".dishCount").html(dishCount + 1);
            $(this).find(".dishCount").removeClass("hidden");
          }
        })
        $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
        $(".order .main-right .beizhu .totalMon").html((totalMon + dishinfo.price * 1).toFixed(2));
        // javascript:showprice.ShowPrice((totalMon + dishinfo.price * 1).toFixed(2).toString());
        $(".order .main .main-left li").each(function (j, k) {
          if ($(this).data("categoryid") == c_id) {
            var typeCount = +$(this).find(".typeCount").html();
            $(this).find(".typeCount").html(typeCount + 1);
            $(this).find(".typeCount").removeClass("hidden");
          }
        });
        dishinfo.num = 1;
        cart.unshift(dishinfo);
        var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
        curTable.cart = cart;
        localStorage.setItem("curTable", JSON.stringify(curTable));
        var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
        if (tableInfos && tableInfos.length > 0) {
          $.each(tableInfos, function (j, k) {
            if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
              // tableInfos[j] = curTable;
              tableInfos.splice(j, 1, curTable);
            }
          })
          localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
        }
        if (!isNaN(sale_count)) {
          sale_count -= 1;
          if (sale_count <= 0) {
            sale_count = 0;
            $(that).find(".sold_out").removeClass("hidden");
          }
          $(that).find(".sale_count").html(sale_count);
        }
      }
    } else {
      $(that).find(".sold_out").removeClass("hidden")
      App.msg(dishinfo.name + "已售罄！");
    }
  })
  //刷新菜品
  $(".btn-refresh").off("click").on("click", function () {
    location.reload(true);
  })

  //添加临时菜
  $(".dish_ls").off("click").on("click", function () {
    //获取菜品分类，单位，打印机
    $("#dishname").val("");
    $("#dishprice").val("");
    $("#dishnum").val("1.0");
    App.ajax("ss_wx/get_pos_category", "get", {}, function (res) {
      if (res.flag == true) {
        var categoryHtml = "",
          unitHtml = "",
          printHtml = "";
        $.each(res.category, function (i, v) {
          categoryHtml += '<button data-categoryinfo=\'' + JSON.stringify(v) + '\' class="btn">' + v.name + '</button>';
        });
        $.each(res.uomcollection, function (i, v) {
          unitHtml += '<button data-unitinfo=\'' + JSON.stringify(v) + '\' class="btn">' + v.name + '</button>';
        });
        $.each(res.printer, function (i, v) {
          printHtml += '<button data-printinfo=\'' + JSON.stringify(v) + '\' class="btn">' + v.name + '</button>';
        });

        $("#modal_ls .dishlbBox").html(categoryHtml);
        $("#modal_ls .unitBox").html(unitHtml);
        $("#modal_ls .printBox").html(printHtml);
      } else {
        App.msg(res.msg)
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true);
  });
  //临时菜 份数减
  $("#modal_ls .icon-minus-circle").off("click").on("click", function () {
    var num = $('#modal_ls #dishnum').val() * 1;
    num -= 1;
    if (num <= 1) {
      $('#modal_ls #dishnum').val("1.0");
    } else {
      $('#modal_ls #dishnum').val(num.toFixed(1));
    }
  });
  $('#modal_ls').on('shown.bs.modal', function () {
    $('#dishname').focus()
  })
  //临时菜 份数加
  $("#modal_ls .icon-plus-circle").off("click").on("click", function () {
    var num = $('#modal_ls #dishnum').val() * 1;
    $('#modal_ls #dishnum').val((num + 1).toFixed(1));
  });
  //临时菜 选择单位
  $("#modal_ls .unitBox").off("click").on("click", ".btn", function () {
    $(this).addClass("choosed").siblings().removeClass("choosed");
  });
  /*
  //临时菜 选择菜品分类
  $("#modal_ls .dishlbBox").off("click").on("click", ".btn", function () {
    $(this).addClass("choosed").siblings().removeClass("choosed");
  });
   */
  //临时菜 选择打印机
  $("#modal_ls .printBox").off("click").on("click", ".btn", function () {
    $(this).toggleClass("choosed");
  });

  //临时菜  确定按钮
  $("#modal_ls .btn-sure").off("click").on("click", function () {
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    var dishname = $("#dishname").val();
    var price = $("#dishprice").val() * 1;
    var dishnum = $("#dishnum").val() * 1;
    var unit = $(".unitBox .choosed").data("unitinfo");
    var print = [];
    $(".printBox .choosed").each(function (i, v) {
      var printinfo = $(this).data("printinfo");
      print.push(printinfo);

    });
    if (dishname == "") {
      App.msg("请输入菜品名称");
    } else if (price === "") {
      App.msg("请输入菜品价格");
    } else if (!unit) {
      App.msg("请选择菜品单位");
    } else {
      var dishinfo = {
        // "c_id": category.id,
        "temporary_flag": true,
        "default_code": "",
        "name": dishname,
        "pinyin_str": "",
        "sale_out_state": false,
        "p_id": "",
        "cuisine": [],
        "price": price,
        "num": dishnum,
        print: print,
        uom: unit.name
      };
      var html = "<li data-dishes='" + JSON.stringify(dishinfo) + "' data-price='" + price + "' data-pid=''><div class='ls_dish'>临时</div><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div> <div class='dishName'>" + dishname + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle'></i><span class='dishNum'>" + dishnum + "</span> <i data-control='plus' class='iconfont icon-plus-circle'></i> </div><div class='dishPrice'>" + (price * 1 * dishnum).toFixed(2) + "</div></li>";
      $(".order .main-right .dishList").prepend(html);
      var totalCount = +$(".order .main-right .beizhu .totalCount").html();
      var totalMon = +$(".order .main-right .beizhu .totalMon").html();
      $(".order .main-right .beizhu .totalCount").html((totalCount + dishnum))
      $(".order .main-right .beizhu .totalMon").html((totalMon + (price * dishnum)).toFixed(2));
      cart.unshift(dishinfo);
      var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
      curTable.cart = cart;
      localStorage.setItem("curTable", JSON.stringify(curTable));
      var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
      if (tableInfos && tableInfos.length > 0) {
        $.each(tableInfos, function (j, k) {
          if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
            // tableInfos[j] = curTable;
            tableInfos.splice(j, 1, curTable);
          }
        })
        localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
      }
      total();
      $("#modal_ls").modal("hide");

    }
  })


  //点击菜品加入购物车并缓存
  $(".order .mainBox .main-content").off("click").on("click", "li", function () {
    var dishinfo = $(this).data("dishinfo");
    var that = this;
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    if (dishinfo == "search") {
      //点击搜索
      page = "search";
      $(".mainBox .tipBox").addClass("hidden");
      // var Products = JSON.parse(localStorage.getItem("category_product"));
      $(".order .main-left li:first").addClass("active").siblings().removeClass("active");
      var typeId = $(".order .main-left li.active").data("categoryid");
      $(".mainBox .main-content").addClass("hidden");
      $(".mainBox .main-search").removeClass("hidden");
      $(".mainBox .main-search .btnMiss").off("click").on("click", function () {
        $(".mainBox .tipBox").removeClass("hidden");
        sear_btnmiss = false;
        $("#searKey").val("");
        page = "order";
        $(".mainBox .main-content").removeClass("hidden");
        $(".mainBox .main-search").addClass("hidden");
        $(".mainBox .main-search .searList").empty();
        $(".main-left li:first").addClass("active").siblings().removeClass("active");
        pageAllNum = 1;
        getAll("Dish", "", "all");
      })
    } else {
      var sale_count = $(this).find(".sale_count").html() * 1;
      if (sale_count > 0 || isNaN(sale_count)) {
        if (dishinfo.cuisine && dishinfo.cuisine.length > 0) {
          //显示做法弹框
          $(".ZFModal .title .dishname").html(dishinfo.name);
          $(".ZFModal").removeClass("hidden");
          //显示做法
          var ZFHtml = "";
          $.each(dishinfo.cuisine, function (x, y) {
            ZFHtml += '<button class="btn1" data-zfinfo=\'' + JSON.stringify(y) + '\'>' + y.name + '</button>'
          })
          $(".ZFModal .Body").html(ZFHtml);
          $(".ZFModal .footer .price").html(dishinfo.price);

          //选择做法
          $(".ZFModal .Body").off("click").on("click", ".btn1", function () {
            var zfInfo = $(this).data("zfinfo");
            var zfmodal_price = $(".ZFModal .footer .price").html() * 1;
            if (!$(this).hasClass('active')) {
              $(".ZFModal .footer .price").html(parseFloat(zfmodal_price + zfInfo.price * 1).toFixed(2));
              $(this).addClass('active');
            } else {
              $(".ZFModal .footer .price").html(parseFloat(zfmodal_price - zfInfo.price).toFixed(2));
              $(this).removeClass('active');
            }
          })
          //点击做法确定按钮
          $(".ZFModal .footer .btn-sure").off("click").on("click", function () {
            var tastes = [];
            $(".ZFModal .Body .active").each(function (i, v) {
              tastes.push($(this).data("zfinfo"));
            })
            dishinfo.ZFprice = 0;
            dishinfo.ZFname = "";
            dishinfo.tastes = tastes;
            if (tastes.length == 0) {
              dishinfo.ZFprice = 0;
              dishinfo.ZFname = "";
            } else {
              tastes.forEach(function (v, i) {
                dishinfo.ZFprice += v.price * 1;
                dishinfo.ZFname += v.name + " ";
              })
            }
            dishinfo.num = 1;
            dishinfo.totalTCPrice = (dishinfo.price * dishinfo.num).toFixed(2);
            $(".ZFModal").addClass("hidden");
            var html = "<li data-dishes='' data-price='" +
                (dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2) +
                "' data-defaultcode='" + dishinfo.default_code + "' data-pid='" + dishinfo.p_id + "' data-cid='" +
                dishinfo.c_id + "'><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫" +
                "</div> <div class='dishName'>" + dishinfo.name +"<span style='color:#f40'>"+
                (dishinfo.ZFname ? " | " + dishinfo.ZFname : "") + "</span></div><div class='control'>" +
                " <i data-control='minus' class='iconfont icon-minus-circle'></i><span class='dishNum'>1</span>" +
                " <i data-control='plus' class='iconfont icon-plus-circle'></i> </div><div class='dishPrice'>" +
                (dishinfo.price * 1 + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2) + "</div></li>"
            $(".order .main-right .dishList").prepend(html);
            var totalCount = +$(".order .main-right .beizhu .totalCount").html();
            var totalMon = +$(".order .main-right .beizhu .totalMon").html();
            var dishCount = +$(that).find(".dishCount").html();
            var c_id = dishinfo.c_id;
            $(that).find(".dishCount").html(dishCount + 1);
            $(that).find(".dishCount").removeClass("hidden");
            $(".order .main-right .beizhu .totalCount").html((totalCount + 1));
            $(".order .main-right .beizhu .totalMon").html((totalMon + dishinfo.price + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2));
            // javascript:showprice.ShowPrice((totalMon + dishinfo.price + (dishinfo.ZFprice ? dishinfo.ZFprice * 1 : 0)).toFixed(2).toString());
            $(".order .main .main-left li").each(function (j, k) {
              if ($(this).data("categoryid") == c_id) {
                var typeCount = +$(this).find(".typeCount").html();
                $(this).find(".typeCount").html(typeCount + 1);
                $(this).find(".typeCount").removeClass("hidden");
              }
            });
            cart.unshift(dishinfo);
            var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
            curTable.cart = cart;
            localStorage.setItem("curTable", JSON.stringify(curTable));
            var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
            if (tableInfos && tableInfos.length > 0) {
              $.each(tableInfos, function (j, k) {
                if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                  // tableInfos[j] = curTable;
                  tableInfos.splice(j, 1, curTable);
                }
              })
              localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
            }
            total();
            if (!isNaN(sale_count)) {
              sale_count -= 1;
              if (sale_count <= 0) {
                sale_count = 0;
                $(that).find(".sold_out").removeClass("hidden");
              }
              $(that).find(".sale_count").html(sale_count);
            }
          })


        }
        else if (dishinfo.combo_details && dishinfo.combo_details.length > 0) {
          // if(App.timeBetween(dishinfo.start_sale_date,dishinfo.end_sale_date)){
          //显示套餐弹框
          $(".TCModal .title .dishname").html(dishinfo.name);
          $(".TCModal").removeClass("hidden");
          //显示套餐
          var TCHtml = "";
          var MRprice = 0;
          $.each(dishinfo.combo_details, function (x, y) {
            // TCHtml += '<button class="btn1 ' + (y.category_leixing == "默认全选" ? "active" : "") + '" data-tcinfo=\'' + JSON.stringify(y) + '\'>' + y.name + (y.category_leixing == "默认全选" ? "<i class='iconfont icon-bixuan'></i>" : " ") + '</button>';
            TCHtml += '<div data-tcinfo=\'' + JSON.stringify(y) + '\' class="tcItem"><div class="category_name">' + y.category_name + (y.category_leixing == "默认全选" ? '(必选)' : '(任选' + y.select_num + '份)') + '</div><div class="tcDisheList">';
            $.each(y.menu_list, function (a, b) {
              if (y.category_leixing == "默认全选") {
                MRprice += b.price * b.qty;
              }
              TCHtml += '<div class="tcDishe"><span class="dishname">' + b.name + '</span>' +
                  '<span class="price">￥' + b.price.toFixed(2) + '</span>' +
                  '<div class="control" data-dishinfo1=\'' + JSON.stringify(b) + '\'> ' +
                  '<span  class="iconfont icon-minus-circle minus ' + (y.category_leixing == "默认全选" ? "hidden" : "") + '">' +
                  '</span> <span class="num">' + ((y.category_leixing == "默认全选" ? b.qty : "0")) + '</span>' +
                  '<span class="iconfont icon-plus-circle ' + (y.category_leixing == "默认全选" ? "hidden" : "") + ' add">' +
                  '</span></div></div>'
            })
            TCHtml += '</div></div>';

          });

          $(".TCModal .Body").html(TCHtml);
          $(".TCModal .footer .price").html(dishinfo.extend_combo_flag ? dishinfo.price.toFixed(2) : MRprice.toFixed(2));
          dishinfo.totalTCPrice = (dishinfo.extend_combo_flag ? dishinfo.price.toFixed(2) : MRprice.toFixed(2));
          //选择套餐(套餐的一种写法)
          // $(".TCModal .Body").off("click").on("click", ".btn1", function () {
          //   var totalTCPrice = $(".TCModal .footer .price").html();
          //   var tcInfo = $(this).data("tcinfo");
          //   if (tcInfo.category_leixing == "默认全选") {
          //     App.msg("所选菜品为该套餐必选菜品");
          //   } else {
          //     // $(this).addClass("active").siblings().removeClass("active");
          //     $(this).toggleClass("active");
          //     if ($(this).hasClass("active")) {
          //       $(".TCModal .footer .price").html(totalTCPrice * 1 + tcInfo.price * 1);
          //       totalTCPrice = totalTCPrice * 1 + tcInfo.price * 1;
          //     } else {
          //       $(".TCModal .footer .price").html(totalTCPrice * 1 - tcInfo.price * 1);
          //       totalTCPrice = totalTCPrice * 1 - tcInfo.price * 1;
          //     }
          //   }
          //   dishinfo.totalTCPrice = totalTCPrice;
          // })
          //选择套餐（套餐的另一种写法）//点击加号
          $(".TCModal .Body").off("click").on("click", ".add", function () {
            var dishinfo1 = $(this).parents(".control").data("dishinfo1");
            var tcprice = $(".TCModal .footer .price").html();
            //本菜品可选的套餐的最大数量
            var dish_select_num = dishinfo1.select_num;
            var tcinfo = $(this).parents(".tcItem").data("tcinfo");
            //本套餐的这个类别最多可选的数量
            var choosedNum = 0;
            $(this).parents(".tcItem").find(".num").each(function (x, y) {
              if ($(this).html() > 0) {
                choosedNum += 1;
              }
            })
            var tc_select_num = tcinfo.select_num;
            var num = $(this).prev().html() * 1;
            if (num <= 0) {
              if (choosedNum < tc_select_num) {
                $(this).prev().html(num + 1);
                $(".TCModal .footer .price").html((tcprice * 1 + dishinfo1.price).toFixed(2));
                dishinfo.totalTCPrice = (tcprice * 1 + dishinfo1.price).toFixed(2);
                dishinfo1.categoryNum = 0;
                dishinfo1.categoryNum += 1;
                dishinfo1.choosedNum = num + 1;
                $(this).parents(".control").attr("data-dishinfo1", JSON.stringify(dishinfo1));
              } else {
                App.msg(tcinfo.category_name + "只能任选" + tcinfo.select_num + "份");
              }
            } else {
              if (num < dish_select_num) {
                $(this).prev().html(num + 1);
                $(".TCModal .footer .price").html((tcprice * 1 + dishinfo1.price).toFixed(2));
                dishinfo.totalTCPrice = (tcprice * 1 + dishinfo1.price).toFixed(2);
                dishinfo1.choosedNum = num + 1;
                $(this).parents(".control").attr("data-dishinfo1", JSON.stringify(dishinfo1));
              } else {
                App.msg("本菜品只可选择" + dish_select_num + "份")
              }

            }
          })
          // //点击减号
          $(".TCModal .Body").on("click", ".minus", function () {
            var dishinfo1 = $(this).parents(".control").data("dishinfo1");
            var tcprice = $(".TCModal .footer .price").html();
            var num = $(this).next().html() * 1;
            // var choosedNum = 0;
            // $(this).parents(".tcItem").find(".num").each(function (x, y) {
            //   if($(this).html()>0)
            //   choosedNum +=1;
            // })
            if (num <= 0) {
              return;
            } else {
              $(this).next().html(num - 1);
              $(".TCModal .footer .price").html((tcprice - dishinfo1.price).toFixed(2));
              dishinfo.totalTCPrice = (tcprice * 1 - dishinfo1.price).toFixed(2);
              if (num - 1 <= 0) {
                dishinfo1.categoryNum = 0;
              }
              dishinfo1.choosedNum = num - 1;
              $(this).parents(".control").attr("data-dishinfo1", JSON.stringify(dishinfo1));
            }
          })
          //点击套餐确定按钮
          $(".TCModal .footer .btn-sure").off("click").on("click", function () {
            if (dishinfo.extend_combo_flag) {
              dishinfo.totalTCPrice = dishinfo.price;
            }
            dishinfo.combo_details1 = [];
            var flag1 = "";
            $(".TCModal .Body .tcItem").each(function (x, y) {
              var tcinfo = $(this).data("tcinfo");
              if (tcinfo.category_leixing == "默认全选") {
                flag1 += "true";
              }
              var tcChoosedNum = 0;
              if (tcinfo.category_leixing == "客人自选") {
                tcinfo.menu_list = [];
                $(this).find('.control').each(function (a, b) {
                  var dishinfo1 = $(this).data("dishinfo1");

                  if (dishinfo1.categoryNum >= 1) {
                    tcinfo.menu_list.push(dishinfo1);
                  }
                  tcChoosedNum += (dishinfo1.categoryNum ? dishinfo1.categoryNum * 1 : 0);

                });
                if (tcChoosedNum == tcinfo.select_num) {
                  flag1 += "true";
                } else {
                  flag1 += "false";
                  App.msg(tcinfo.category_name + "需任选" + tcinfo.select_num + "份");
                  return false;
                }
              }
              dishinfo.combo_details1.push(tcinfo);
            });
            if (flag1.indexOf("false") == -1) {
              $(".TCModal").addClass("hidden");
              var html = "<li data-dishes=\'" + JSON.stringify(dishinfo) + "\'  data-price='" + dishinfo.totalTCPrice + "' data-pid='" + dishinfo.p_id + "' data-defaultcode='" + dishinfo.default_code + "' data-cid='" + dishinfo.c_id + "'><div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div> <div class='dishName'>" + dishinfo.name + (dishinfo.combo_details && dishinfo.combo_details.length > 0 ? "<a style='margin-left:8px;' class='btn btn-details btn-warning btn-sm' data-toggle='popover'>套餐详情</a>" : "") + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle'></i><span class='dishNum'>1</span> <i data-control='plus' class='iconfont icon-plus-circle'></i> </div><div class='dishPrice'>" + (dishinfo.totalTCPrice * 1).toFixed(2) + "</div></li>"
              $(".order .main-right .dishList").prepend(html);
              var totalCount = +$(".order .main-right .beizhu .totalCount").html();
              var totalMon = +$(".order .main-right .beizhu .totalMon").html();
              var dishCount = +$(that).find(".dishCount").html();
              var c_id = dishinfo.c_id;
              $(that).find(".dishCount").html(dishCount + 1);
              $(that).find(".dishCount").removeClass("hidden");
              $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
              $(".order .main-right .beizhu .totalMon").html((totalMon + dishinfo.totalTCPrice * 1).toFixed(2));
              // javascript:showprice.ShowPrice((totalMon + dishinfo.totalTCPrice * 1).toFixed(2).toString());
              $(".order .main .main-left li").each(function (j, k) {
                if ($(this).data("categoryid") == c_id) {
                  var typeCount = +$(this).find(".typeCount").html();
                  $(this).find(".typeCount").html(typeCount + 1);
                  $(this).find(".typeCount").removeClass("hidden");
                }
              });
              dishinfo.num = 1;
              cart.unshift(dishinfo);
              var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
              curTable.cart = cart;
              localStorage.setItem("curTable", JSON.stringify(curTable));
              var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
              if (tableInfos && tableInfos.length > 0) {
                $.each(tableInfos, function (j, k) {
                  if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                    // tableInfos[j] = curTable;
                    tableInfos.splice(j, 1, curTable);
                  }
                })
                localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
              }
              total();
            }
            if (!isNaN(sale_count)) {
              sale_count -= 1;
              if (sale_count <= 0) {
                sale_count = 0;
                $(that).find(".sold_out").removeClass("hidden")
              }
              $(that).find(".sale_count").html(sale_count);
            }
          })
          // }else{
          //   App.msg("还没到优惠时间哦。")
          // }
        }
        else {
          var html = "<li data-dishes='' data-price='" + dishinfo.price + "' data-defaultcode='" + dishinfo.default_code +
              "' data-pid='" + dishinfo.p_id + "' data-cid='" + dishinfo.c_id + "'>" +
              "<div class='pack hidden'>打包</div><div class='waitCall hidden'>等叫</div> <div class='dishName'>" +
              dishinfo.name + "</div><div class='control'> <i data-control='minus' class='iconfont icon-minus-circle'>" +
              "</i><span class='dishNum'>1</span> <i data-control='plus' class='iconfont icon-plus-circle'></i> " +
              "</div><div class='dishPrice'>" + (dishinfo.price * 1).toFixed(2) + "</div></li>"
          $(".order .main-right .dishList").prepend(html);
          var totalCount = +$(".order .main-right .beizhu .totalCount").html();
          var totalMon = +$(".order .main-right .beizhu .totalMon").html();
          var dishCount = +$(this).find(".dishCount").html();
          var c_id = dishinfo.c_id;
          $(this).find(".dishCount").html(dishCount + 1);
          $(this).find(".dishCount").removeClass("hidden");
          $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
          $(".order .main-right .beizhu .totalMon").html((totalMon + dishinfo.price).toFixed(2));
          // javascript:showprice.ShowPrice((totalMon + dishinfo.price).toFixed(2).toString());
          $(".order .main .main-left li").each(function (j, k) {
            if ($(this).data("categoryid") == c_id) {
              var typeCount = +$(this).find(".typeCount").html();
              $(this).find(".typeCount").html(typeCount + 1);
              $(this).find(".typeCount").removeClass("hidden");
            }
          });
          dishinfo.num = 1;
          dishinfo.totalTCPrice = (parseFloat(dishinfo.price) * parseFloat(dishinfo.num)).toFixed(2);
          cart.unshift(dishinfo);
          var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
          curTable.cart = cart;
          localStorage.setItem("curTable", JSON.stringify(curTable));
          var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          if (tableInfos && tableInfos.length > 0) {
            $.each(tableInfos, function (j, k) {
              if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                // tableInfos[j] = curTable;
                tableInfos.splice(j, 1, curTable);
              }
            })
            localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
          }
          total();
          if (!isNaN(sale_count)) {
            sale_count -= 1;
            if (sale_count <= 0) {
              sale_count = 0;
              $(that).find(".sold_out").removeClass("hidden")
            }
            $(that).find(".sale_count").html(sale_count);
          }
        }
      } else {
        $(that).find(".sold_out").removeClass("hidden")
        App.msg(dishinfo.name + "已售罄！");
      }
    }
  });

  //点击弹框关闭按钮和点击遮罩关闭当前弹框
  $(".icon-shanchu").off("click").on("click", function () {
    $(".ZFModal").addClass("hidden");
    $(".TCModal").addClass("hidden");
  })
  $(".modal_shadow").off("click").on("click", function () {
    $(".ZFModal").addClass("hidden");
    $(".TCModal").addClass("hidden");
  })

  //点击购物车加减改变数量
  $(".order .main-right .dishList").on("click", ".iconfont", function () {
    var that = this;
    var dishes = $(this).parents("li").data("dishes");
    var temporary_flag = false;
    if (dishes !== "") {
      temporary_flag = dishes.temporary_flag;
    }
    var cart1 = JSON.parse(localStorage.getItem("curTable") || "{}").cart;
    var control = $(this).data("control");
    var dishNum = "";
    var dishPrice = $(this).parents("li").data("price");
    var default_code = $(this).parents("li").data("defaultcode");
    var ind = $(this).parents("li").index();
    var dishName = $(this).parents("li").find(".dishName").html();
    var p_id = $(this).parents("li").data("pid");
    var c_id = $(this).parents("li").data("cid");
    var totalCount = +$(".order .main-right .beizhu .totalCount").html();
    var totalMon = +$(".order .main-right .beizhu .totalMon").html();
    if (control == "plus") {
      if (page == "order") {
        if (temporary_flag) {
          //点击加号
          dishNum = +$(that).prev().html();
          dishNum += 1;
          $(that).prev().html(dishNum);
          $(that).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
          $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
          $(".order .main-right .beizhu .totalMon").html((totalMon + dishPrice * 1).toFixed(2));
          // javascript:showprice.ShowPrice((totalMon + dishPrice * 1).toFixed(2).toString());
          cart1[ind].num += 1;
          var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
          curTable.cart = cart1;
          localStorage.setItem("curTable", JSON.stringify(curTable));

          var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          if (tableInfos && tableInfos.length > 0) {
            $.each(tableInfos, function (j, k) {
              if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                tableInfos.splice(j, 1, curTable);
              }
            })
            localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
          }
          total();
        }
        else {
          $(".order .mainBox .main-content li").each(function (a, b) {
            var dishInfo = $(this).data("dishinfo");
            var name = $(this).find(".dishName").html();
            var dishId = dishInfo.p_id;
            var defaueCode = dishInfo.default_code;
            var dishCount = +$(this).find(".dishCount").html();
            var sale_count = +$(this).find(".sale_count").html();
            if (temporary_flag || (p_id == dishId && defaueCode == default_code)) {
              if (!isNaN(sale_count) && !temporary_flag) {
                if (sale_count <= 0) {
                  $(this).find(".sold_out").removeClass("hidden");
                  $(this).find(".sale_count").html(0);
                  $(this).find(".dishCount").html(dishCount);
                  App.msg(name + "没有库存了！")
                } else {
                  $(this).find(".sale_count").html(sale_count - 1);
                  if (sale_count - 1 <= 0) {
                    $(this).find(".sold_out").removeClass("hidden");
                  }
                  //点击加号
                  $(this).find(".dishCount").html(dishCount + 1);
                  dishNum = +$(that).prev().html();
                  dishNum += 1;
                  $(that).prev().html(dishNum);
                  $(that).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
                  $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
                  $(".order .main-right .beizhu .totalMon").html((totalMon + dishPrice * 1).toFixed(2));
                  // javascript:showprice.ShowPrice((totalMon + dishPrice * 1).toFixed(2).toString());
                  $(".order .main .main-left li").each(function (j, k) {
                    if ($(this).data("categoryid") == c_id) {
                      var typeCount = +$(this).find(".typeCount").html();
                      $(this).find(".typeCount").html(typeCount + 1);
                      $(this).find(".typeCount").removeClass("hidden");
                    }
                  });
                  cart1[ind].num += 1;
                  var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
                  curTable.cart = cart1;
                  localStorage.setItem("curTable", JSON.stringify(curTable));

                  var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                  if (tableInfos && tableInfos.length > 0) {
                    $.each(tableInfos, function (j, k) {
                      if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                        // tableInfos[j] = curTable;
                        tableInfos.splice(j, 1, curTable);
                      }
                    })
                    localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
                  }
                  total();
                }
              } else {
                $(this).find(".dishCount").html(dishCount + 1);
                dishNum = +$(that).prev().html();
                dishNum += 1;
                $(that).prev().html(dishNum);
                $(that).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
                $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
                $(".order .main-right .beizhu .totalMon").html((totalMon + dishPrice * 1).toFixed(2));
                // javascript:showprice.ShowPrice((totalMon + dishPrice * 1).toFixed(2).toString());
                $(".order .main .main-left li").each(function (j, k) {
                  if ($(this).data("categoryid") == c_id) {
                    var typeCount = +$(this).find(".typeCount").html();
                    $(this).find(".typeCount").html(typeCount + 1);
                    $(this).find(".typeCount").removeClass("hidden");
                  }
                });
                cart1[ind].num += 1;
                var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
                curTable.cart = cart1;
                localStorage.setItem("curTable", JSON.stringify(curTable));

                var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                if (tableInfos && tableInfos.length > 0) {
                  $.each(tableInfos, function (j, k) {
                    if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                      // tableInfos[j] = curTable;
                      tableInfos.splice(j, 1, curTable);
                    }
                  })
                  localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
                }
                total();
              }

            }
          })
        }
      }
      else {
        if (temporary_flag) {
          //点击加号
          dishNum = +$(that).prev().html();
          dishNum += 1;
          $(that).prev().html(dishNum);
          $(that).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
          $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
          $(".order .main-right .beizhu .totalMon").html((totalMon + dishPrice * 1).toFixed(2));
          // javascript:showprice.ShowPrice((totalMon + dishPrice * 1).toFixed(2).toString());
          cart1[ind].num += 1;
          var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
          curTable.cart = cart1;
          localStorage.setItem("curTable", JSON.stringify(curTable));

          var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          if (tableInfos && tableInfos.length > 0) {
            $.each(tableInfos, function (j, k) {
              if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                // tableInfos[j] = curTable;
                tableInfos.splice(j, 1, curTable);
              }
            })
            localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
          }
          total();
        }else {
          $(".order .mainBox .main-search li").each(function (a, b) {
            var dishInfo = $(this).data("dishinfo");
            var name = $(this).find(".dishName").html();
            var dishId = dishInfo.p_id;
            var dishCount = +$(this).find(".dishCount").html();
            var defaueCode = dishInfo.default_code;
            var sale_count = +$(this).find(".sale_count").html();
            if (p_id == dishId && defaueCode == default_code) {
              // sale_count -= 1;
              // dishCount += 1;
              if (!isNaN(sale_count)) {
                if (sale_count <= 0) {
                  $(this).find(".sold_out").removeClass("hidden");
                  $(this).find(".sale_count").html(0);
                  $(this).find(".dishCount").html(dishCount);
                  App.msg(name + "没有库存了！")
                } else {
                  $(this).find(".sale_count").html(sale_count - 1);
                  if (sale_count - 1 <= 0) {
                    $(this).find(".sold_out").removeClass("hidden");
                  }
                  //点击加号
                  $(this).find(".dishCount").html(dishCount + 1);
                  dishNum = +$(that).prev().html();
                  dishNum += 1;
                  $(that).prev().html(dishNum);
                  $(that).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
                  $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
                  $(".order .main-right .beizhu .totalMon").html((totalMon + dishPrice * 1).toFixed(2));
                  // javascript:showprice.ShowPrice((totalMon + dishPrice * 1).toFixed(2).toString());
                  $(".order .main .main-left li").each(function (j, k) {
                    if ($(this).data("categoryid") == c_id) {
                      var typeCount = +$(this).find(".typeCount").html();
                      $(this).find(".typeCount").html(typeCount + 1);
                      $(this).find(".typeCount").removeClass("hidden");
                    }
                  });
                  cart1[ind].num += 1;
                  var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
                  curTable.cart = cart1;
                  localStorage.setItem("curTable", JSON.stringify(curTable));

                  var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                  if (tableInfos && tableInfos.length > 0) {
                    $.each(tableInfos, function (j, k) {
                      if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                        // tableInfos[j] = curTable;
                        tableInfos.splice(j, 1, curTable);
                      }
                    })
                    localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
                  }
                  total();
                }
              } else {
                //点击加号
                $(this).find(".dishCount").html(dishCount + 1);
                dishNum = +$(that).prev().html();
                dishNum += 1;
                $(that).prev().html(dishNum);
                $(that).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
                $(".order .main-right .beizhu .totalCount").html((totalCount + 1))
                $(".order .main-right .beizhu .totalMon").html((totalMon + dishPrice * 1).toFixed(2));
                // javascript:showprice.ShowPrice((totalMon + dishPrice * 1).toFixed(2).toString());
                $(".order .main .main-left li").each(function (j, k) {
                  if ($(this).data("categoryid") == c_id) {
                    var typeCount = +$(this).find(".typeCount").html();
                    $(this).find(".typeCount").html(typeCount + 1);
                    $(this).find(".typeCount").removeClass("hidden");
                  }
                });
                cart1[ind].num += 1;
                var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
                curTable.cart = cart1;
                localStorage.setItem("curTable", JSON.stringify(curTable));

                var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
                if (tableInfos && tableInfos.length > 0) {
                  $.each(tableInfos, function (j, k) {
                    if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                      // tableInfos[j] = curTable;
                      tableInfos.splice(j, 1, curTable);
                    }
                  })
                  localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
                }
                total();
              }
            }
          })
        }
      }

    } else {
      // 点击减号
      dishNum = +$(this).next().html();
      dishNum -= 1;
      if (dishNum == 0) {
        $(this).parents("li").remove();
      }
      $(this).next().html(dishNum);
      $(this).parents("li").find(".dishPrice").html((dishPrice * dishNum).toFixed(2));
      $(".order .main-right .beizhu .totalCount").html((totalCount - 1))
      $(".order .main-right .beizhu .totalMon").html((totalMon - dishPrice).toFixed(2));
      // javascript:showprice.ShowPrice((totalMon - dishPrice).toFixed(2).toString());
      $(".order .main .main-left li").each(function (j, k) {
        if ($(this).data("categoryid") == c_id) {
          var typeCount = +$(this).find(".typeCount").html();
          typeCount -= 1;
          $(this).find(".typeCount").html(typeCount);
          if (typeCount == 0) {
            $(this).find(".typeCount").addClass("hidden");
          } else {
            $(this).find(".typeCount").removeClass("hidden");
          }
        }
      });
      if (page == "order") {
        $(".order .mainBox .main-content li").each(function (a, b) {
          var dishInfo = $(this).data("dishinfo");
          var name = $(this).find(".dishName").html();
          var dishId = dishInfo.p_id;
          var defaueCode = dishInfo.default_code;
          var dishCount = +$(this).find(".dishCount").html();
          var sale_count = +$(this).find(".sale_count").html();
          dishCount -= 1;
          if (p_id == dishId && default_code == defaueCode) {
            $(this).find(".dishCount").html(dishCount);
            if (!isNaN(sale_count)) {
              $(this).find(".sale_count").html(sale_count + 1);
              if (sale_count >= 0) {
                $(this).find(".sold_out").addClass("hidden");
              }
            }

            if (dishCount == 0) {
              $(this).find(".dishCount").addClass("hidden");
            }
          }
        })
      } else {
        $(".order .mainBox .main-search li").each(function (a, b) {
          var dishInfo = $(this).data("dishinfo");
          var name = $(this).find(".dishName").html();
          var dishId = dishInfo.p_id;
          var defaueCode = dishInfo.default_code;
          var dishCount = +$(this).find(".dishCount").html();
          var sale_count = +$(this).find(".sale_count").html();
          dishCount -= 1;
          if (p_id == dishId && defaueCode == default_code) {
            $(this).find(".dishCount").html(dishCount);
            if (!isNaN(sale_count)) {
              $(this).find(".sale_count").html(sale_count + 1);
              if (sale_count >= 0) {
                $(this).find(".sold_out").addClass("hidden");
              }
            }
            if (dishCount == 0) {
              $(this).find(".dishCount").addClass("hidden");
            }
          }
        })
      }

      cart1[ind].num -= 1;
      if (cart1[ind].num == 0) {
        cart1.splice(ind, 1);
      };
      var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
      curTable.cart = cart1;
      localStorage.setItem("curTable", JSON.stringify(curTable));
      // localStorage.setItem("cart", JSON.stringify(cart1));
      var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
      if (tableInfos && tableInfos.length > 0) {
        $.each(tableInfos, function (j, k) {
          if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
            // tableInfos[j] = curTable;
            tableInfos.splice(j, 1, curTable);
          }
        })
        localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
      }
      total();
    }
  })

  //点击购物车套餐详情
  $(".order .main-right .dishList").off("mouseover").on("mouseover", ".btn-details", function () {
    var dishes = $(this).parents("li").data("dishes");
    var content = "";
    var menu_list = [];
    dishes.combo_details1.forEach(function (x, y) {
      x.menu_list.forEach(function (a, b) {
        menu_list.push(a);
      })
    })
    menu_list.forEach(function (a, b) {
      content += b + 1 + "." + a.name + '---' + (a.choosedNum ? a.choosedNum : a.select_num) + a.uom + '---' + a.price + '元/' + a.uom + "<br/>"
    })
    $(this).popover({
      content: content,
      placement: "left",
      trigger: "click",
      container: 'body',
      html: true
    })
  });

  //点击清空购物车清空缓存
  $(".order .main-right .checkGroup .btnEmpty").on("click", function () {
    var cart = JSON.parse(localStorage.getItem("curTable") || "{}").cart || [];
    if (cart.length > 0) {
      var orderedCart = cart.filter(function (v) {
        return v.rsta == 1
      });
      if (orderedCart.length > 0) {
        // App.confirm("只能清除未下单菜品,确定清除？",function(){

        // })
        App.msg("不能清空已下单菜品。");
      } else {
        App.confirm("确定清空购物车？", function () {
          $(".order .main-right .dishList").empty();
          var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
          curTable.totalInfo = {};
          curTable.p_total = "";
          curTable.cart = [];
          localStorage.setItem("curTable", JSON.stringify(curTable));
          var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
          if (tableInfos && tableInfos.length > 0) {
            $.each(tableInfos, function (j, k) {
              if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
                // tableInfos[j] = curTable;
                tableInfos.splice(j, 1, curTable);
              }
            })
            localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
          }
          $(".order .main-left li").each(function (i, v) {
            $(this).find(".typeCount").html(0).addClass("hidden");
          });
          $(".order .main-content li").each(function (i, v) {
            $(this).find(".dishCount").html(0).addClass("hidden");
          });
          $(".order .main-right .totalCount").html(0.0);
          $(".order .main-right .totalMon").html(0.0);
          location.reload();
        })
      }
    }
  });
  //获取总数量、总金额
  function total() {
    var totalInfo = {};
    totalInfo.totalCount = $(".order .main-right .totalCount").html();
    totalInfo.totalMon = $(".order .main-right .totalMon").html();
    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
    curTable.p_total = totalInfo.totalMon;
    curTable.totalInfo = totalInfo;
    localStorage.setItem("curTable", JSON.stringify(curTable));
    var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    if (tableInfos && tableInfos.length > 0) {
      $.each(tableInfos, function (j, k) {
        if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
          // tableInfos[j] = curTable;
          tableInfos.splice(j, 1, curTable);

        }
      })
      localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
    }
  }

  //跳转支付
  $(".payBtns").on("click", ".btn", function () {
    var totalCount = $(".order .main-right .totalCount").html();
    var code = $(this).data("code");
    localStorage.setItem("payCode", code);
    if (totalCount > 0) {
      // var cart=JSON.parse(localStorage.getItem("cart")||"[]");
      // var totalInfo=JSON.parse(localStorage.getItem("totalInfo")||"{}");
      // var curTable=JSON.parse(localStorage.getItem("curTable")||"{}");
      // curTable.cart=cart;
      // curTable.totalInfo=totalInfo;
      // localStorage.removeItem("cart");
      // localStorage.removeItem("totalInfo");
      // localStorage.setItem("curTable",JSON.stringify(curTable));
      location.href = "Cashier_1.html"
    } else {
      $("#XDBtn").attr("disabled", false);
      App.msg("购物车空空如也！！！");
    }
  });

  //整单打包
  $(".order .main-right .checkGroup #pack").off("click").on("click", function () {
    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
    if (curTable.cart.length <= 0) {
      return
    }
    var checked = $(this).prop("checked");
    if (checked) {
      curTable.pack = true;
      $(".order .main-right .dishList  .pack").removeClass("hidden");
    } else {
      curTable.pack = false;
      $(".order .main-right .dishList  .pack").addClass("hidden");
    }
    localStorage.setItem("curTable", JSON.stringify(curTable));
    var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    if (tableInfos && tableInfos.length > 0) {
      $.each(tableInfos, function (j, k) {
        if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
          // tableInfos[j] = curTable;
          tableInfos.splice(j, 1, curTable);
        }
      })
      localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
    }
  })
  //整单等叫
  $(".order .main-right .checkGroup #waitCall").off("click").on("click", function () {
    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
    if (curTable.cart.length <= 0) {
      return
    }
    var checked = $(this).prop("checked");
    if (checked) {
      curTable.waitCall = true;
      $(".order .main-right .dishList  .waitCall").removeClass("hidden");
    } else {
      curTable.waitCall = false;
      $(".order .main-right .dishList  .waitCall").addClass("hidden");
    }
    localStorage.setItem("curTable", JSON.stringify(curTable));
    var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    if (tableInfos && tableInfos.length > 0) {
      $.each(tableInfos, function (j, k) {
        if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
          // tableInfos[j] = curTable;
          tableInfos.splice(j, 1, curTable);
        }
      })
      localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
    }
  })

  //整单备注
  $(".BZbtn").off("click").on("click", function () {
    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
    var BeiZhu = curTable.ZDBZ || "";
    $("#BeiZhu").val(BeiZhu);
  })
  $('#BZModal').on('shown.bs.modal', function () {
    $('#BeiZhu').focus()
  })
  //点击整单备注确定按钮
  $("#BZModal  .btn-sure").off("click").on("click", function () {
    var Beizhu = $("#BZModal #BeiZhu").val();
    var curTable = JSON.parse(localStorage.getItem("curTable") || "{}");
    curTable.ZDBZ = Beizhu;
    localStorage.setItem("curTable", JSON.stringify(curTable));
    var tableInfos = JSON.parse(localStorage.getItem("tableInfos") || "[]");
    if (tableInfos && tableInfos.length > 0) {
      $.each(tableInfos, function (j, k) {
        if (k.floor_id == curTable.floor_id && k.table_id == curTable.table_id) {
          // tableInfos[j] = curTable;
          tableInfos.splice(j, 1, curTable);
        }
      })
      localStorage.setItem("tableInfos", JSON.stringify(tableInfos));
    }
    $("#BZModal").modal("hide");
  })

  //点击暂存
  $("#ZCBtn").off("click").on("click", function () {

  })


  //点击下单
  $("#XDBtn").off("click").on("click", function () {
    $(this).attr("disabled", true);
    var curTable = JSON.parse(localStorage.getItem("curTable"));
    var tableInfos = JSON.parse(localStorage.getItem("tableInfos"));
    if (curTable.cart && curTable.cart.length > 0) {
      //注释检查其它桌有未上传的订单提示提交订单
      // var hasOrderArr = tableInfos.filter(function (m) {
      //   return m.hasOrder == 1
      // })
      // if(hasOrderArr.length>1){
      //   layer.open({
      //     content:"其它桌台有未上传订单",
      //     btn: ['上传当前桌台订单', '上传全部订单'],
      //     yes:function(index){
      //       App.uploadCurTable(curTable);
      //       layer.close(index);
      //     },
      //     no:function(){
      //       hasOrderArr.forEach(function(m){
      //         App.uploadCurTable(m);
      //       })
      //     }
      //   })
      // }else{
      App.uploadCurTable(curTable);
      // }
    } else {
      $("#XDBtn").attr("disabled", false);
      App.msg("购物车空空如也！！！")
    }
  })

  //点击取单
  $("#QDBtn").off("click").on("click", function () {

  })
  //点击桌台导航
  $(".toTable").off("click").on("click", function () {
    App.ajax("ss_wx/unlock_table", "post", {
      "table_id": curTableInit.table_id
    }, function (res) {
      if (res.flag == true) {
        location.href = "../pages/Table.html"
      } else {
        App.msg("桌台解锁失败");
      }
      if (!res.TokenTrue) {
        App.msg("Token失效，即将跳转到登录页面", function () {
          setTimeout(function () {
            location.href = "../login.html";
          }, 1000)
        })
      }
    }, "", true)

  })

  //点击交班
  App.jiaoBan()

})